Skip to content
Go back

Netty 常用的类

Published:  at  07:13 PM

EventLoopGroup

Netty提供了多种EventLoopGroup实现,如OIO(Blocking I/O)、NIO(New I/O)、Epoll、KQueue等实现。

is used to handle OIO Channel’s. Each Channel will be handled by its own EventLoop to not block others.

is used for NIO Selector based Channels.

uses epoll under the covers. Because of this it only works on linux.

uses kqueue under the covers. Because of this it only works on unix.

EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
	ServerBootstrap b = new ServerBootstrap(); // (2)
	b.group(bossGroup, workerGroup)
	 .channel(NioServerSocketChannel.class) // (3)
	 .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
		 @Override
		 public void initChannel(SocketChannel ch) throws Exception {
			 ch.pipeline().addLast(new DiscardServerHandler());
		 }
	 })
	 .option(ChannelOption.SO_BACKLOG, 128)          // (5)
	 .childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
    
	// Bind and start to accept incoming connections.
	ChannelFuture f = b.bind(port).sync(); // (7)
    // Wait until the server socket is closed.
	// In this example, this does not happen, but you can do that to gracefully
	// shut down your server.
	f.channel().closeFuture().sync();
} finally {
	workerGroup.shutdownGracefully();
	bossGroup.shutdownGracefully();
}

服务端通常使用“boss”来接受连接;用“work”来处理接受的连接的数据传输和已接受的连接的注册处理

ServerBootstrap

a helper class that sets up a server.  You can set up the server using a Channel directly

ChannelInitializer

is a special handler that is purposed to help a user configure a new Channel

ServerBootstrap::childHandler指定的handler每次都会被一个新接受的Channel执行,通常使用它来配置一个Channel的ChannelPipeline

ChannelOptionChannelConfig

set the socket options

AbstractBootstrap::option用来给接受连接的ServerChannel提供配置 ServerBootstrap::childOption用来给被父ServerChannel接受的SocketChannel提供配置

ChannelFutureListener

Listens to the result of a ChannelFuture. The result of the asynchronous Channel I/O operation is notified once this listener is added by calling ChannelFuture.addListener(GenericFutureListener).

netty中的很多操作都是异步的: write、close等等,需要对返回的 ChannelFuture 添加相应的ChannelFutureListener进行后续操作

Bootstrap

A Bootstrap that makes it easy to bootstrap a Channel to use for clients. The bind() methods are useful in combination with connectionless transports such as datagram (UDP). For regular TCP connections, please use the provided connect() methods.

bootstrap是用于客户端的channel

ByteToMessageDecoder

is an implementation of ChannelInboundHandler which makes it easy to deal with the fragmentation issue.

public class TimeDecoder extends ByteToMessageDecoder { // (1)
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { // (2)
        if (in.readableBytes() < 4) {
            return; // (3)
        }
        
        out.add(in.readBytes(4)); // (4)
    }
}

当添加一个对象到out时,此时认为成功解码了一条消息,并且将已读取的部分丢弃

ByteToMessageDecoder会一直调用decode方法,直到没有内容被添加到out中

ReplayingDecoder

 ReplayingDecoder allows you to implement the decode() and decodeLast() methods just like all required bytes were received already, rather than checking the availability of the required bytes.

可以省掉消息是否可用的检查操作,比如:

	if (in.readableBytes() < 4) {
		return; 
	}

Suggest Changes

Previous Post
GitFlow 插件入门
Next Post
RoboFlow视觉